<template>
  <x-dialog :visible="visible" :title="visibleType == 'edit' ? '编辑' : '新增'" @close="handleClose(false)">
    <el-form ref="form" inline :model="form" :rules="rules" label-width="80px">
      <el-form-item label="歌单图片" prop="songListImg" style="display: block">
        <x-img-upload v-model="form.songListImg" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" clearable />
      </el-form-item>
      <el-form-item label="风格" prop="style">
        <x-select
          v-model="form.style"
          :options="[
            { label: '华语', value: '0' },
            { label: '粤语', value: '1' },
            { label: '英语', value: '2' }
          ]"
          :config="{ clearable: true, multiple: true }"
        />
      </el-form-item>
      <el-form-item label="简介" prop="description" style="display: block">
        <el-input
          v-model="form.description"
          cols="70"
          rows="5"
          type="textarea"
          placeholder="请输入内容"
          clearable
        />
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </x-dialog>
</template>

<script>
import * as axiosApi from '../api'
export default {
  props: {
    visible: Boolean,
    visibleType: {
      type: String,
      default: ''
    },
    model: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      form: {
        songListImg: '',
        title: '',
        style: [],
        description: ''
      },
      rules: {
        songListImg: [{ required: true, message: '请输上传歌单图片' }],
        title: [{ required: true, message: '请输入标题' }],
        style: [{ required: true, message: '请输入风格' }]
      }
    }
  },
  watch: {
    visible(newValue, oldValue) {
      if (newValue) {
        Object.keys(this.form).forEach(key => {
          if (this.model[key]) this.form[key] = this.model[key]
        })
      } else {
        this.initForm()
      }
    }
  },
  methods: {
    handleClose(update) {
      this.$emit('close', update)
    },
    initForm() {
      Object.keys(this.form).forEach(item => {
        this.form[item] = ''
      })
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const fun = this.visibleType === 'edit' ? axiosApi.editSongList : axiosApi.addSongList
          const params = {
            ...this.form
          }
          if (this.visibleType === 'edit') params._id = this.model._id
          fun(params).then(() => {
            this.$message({
              message: this.visibleType === 'edit' ? '更新成功' : '新增成功'
            })
            this.handleClose(true)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray: #889aa4;
.show-pwd {
  position: absolute;
  right: 10px;
  top: 2px;
  font-size: 16px;
  color: $dark_gray;
  cursor: pointer;
  user-select: none;
}
</style>
